package com.ny.controller;
import com.ny.entity.Result;
import com.ny.entity.User;
import com.ny.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("/selectAllUser")
    public Result selectAllUser(){
        return Result.success(userService.findAllUser());
    }
    // 登录:http://localhost:8081/login?username=admin&password=<PASSWORD>
    @PostMapping("/login")
    public Result findUser(String username, String password){
        User user = userService.login(username,password);
        if (username != null && password != null && user!=null){
            return Result.success(user);
        }
        return Result.notFound("用户名或密码错误");
    }
    //    根据用户名查询用户信息
//    http://localhost:8081/findUserByUsername?username=admin
    @GetMapping("/findUserByUsername")
    public Result findUserByUsername(String username){
        User user = userService.findUserByUsername(username);
        System.out.println(user);
        if (username != null&&user!=null){
            return Result.success(user);
        }
        return Result.notFound("用户名不存在");
    }
    // 注册
    @PostMapping("/registerUser")
    public Result registerUser(User user){
        return Result.success(userService.registerUser(user));
    }
    
    // 更新用户邮箱
    @PostMapping("/updateEmail")
    public Result updateEmail(int userId, String email) {
        int result = userService.updateEmail(userId, email);
        if (result > 0) {
            return Result.success("邮箱更新成功");
        } else {
            return Result.error("邮箱更新失败");
        }
    }
}
